package com.sugar.real.estate.module.business.dal.mysql.userfollow;

import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.real.estate.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.real.estate.module.business.controller.admin.userfollow.vo.UserFollowRespVO;
import com.sugar.real.estate.module.business.controller.app.apartment.vo.AppApartmentSimpleRespVO;
import com.sugar.real.estate.module.business.controller.app.readyhouse.vo.AppReadyHouseSimpleRespVO;
import com.sugar.real.estate.module.business.controller.app.userfollow.vo.AppUserFollowPageReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.apartment.ApartmentDO;
import com.sugar.real.estate.module.business.dal.dataobject.readyhouse.ReadyHouseDO;
import com.sugar.real.estate.module.business.dal.dataobject.userfollow.UserFollowDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户关注 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface UserFollowMapper extends BaseMapperX<UserFollowDO> {

    @Select("<script>" +
            "select" +
            " count(buf.id)" +
            "from business_user_follow buf " +
            "where buf.deleted = 0 " +
            "<if test=\"param1 != null\"> and buf.user_id = #{param1} </if>" +
            "<if test=\"param2 != null\"> and buf.follow_id = #{param2} </if>" +
            "<if test=\"param3 != null\"> and buf.`type` = #{param3} </if>" +
            "<if test=\"param4 != null and param5 != null\"> and buf.create_time between #{param4} and #{param5} </if>" +
            "</script>")
    Long selectPageCount(Long userId, Long followId, Boolean type, LocalDateTime startTime, LocalDateTime endTime);

    @Select("<script>" +
            "select" +
            " buf.*," +
            " if(buf.`type` = 0, ba.name_zh_cn, brh.name_zh_cn) as name_zh_cn," +
            " if(buf.`type` = 0, ba.name_en_us, brh.name_en_us) as name_en_us " +
            "from business_user_follow buf " +
            "left join business_apartment ba on buf.`type` = 0 and buf.follow_id = ba.id and ba.deleted = 0 " +
            "left join business_ready_house brh on buf.`type` = 1 and buf.follow_id = brh.id and brh.deleted = 0 " +
            "where buf.deleted = 0" +
            "<if test=\"param1 != null\"> and buf.user_id = #{param1} </if>" +
            "<if test=\"param2 != null\"> and buf.follow_id = #{param2} </if>" +
            "<if test=\"param3 != null\"> and buf.`type` = #{param3} </if>" +
            "<if test=\"param4 != null and param5 != null\"> and buf.create_time between #{param4} and #{param5} </if>" +
            "order by buf.id desc " +
            "limit #{param6}, #{param7}" +
            "</script>")
    List<UserFollowRespVO> selectCustomPage(Long userId, Long followId, Boolean type,
                                            LocalDateTime startTime, LocalDateTime endTime, Integer skipCount, Integer pageSize);

    default PageResult<AppReadyHouseSimpleRespVO> appGetUserFollowType1Page(AppUserFollowPageReqVO reqVO, Long userId) {
        return selectJoinPage(reqVO, AppReadyHouseSimpleRespVO.class, new MPJLambdaWrapperX<UserFollowDO>()
                .selectAll(ReadyHouseDO.class)
                .leftJoin(ReadyHouseDO.class, ReadyHouseDO::getId, UserFollowDO::getFollowId)
                .eqIfPresent(UserFollowDO::getUserId, userId)
                .eqIfPresent(UserFollowDO::getType, 1)
                .orderByDesc(ApartmentDO::getId));
    }

    default PageResult<AppApartmentSimpleRespVO> appGetUserFollowType0Page(AppUserFollowPageReqVO reqVO, Long userId) {
        return selectJoinPage(reqVO, AppApartmentSimpleRespVO.class, new MPJLambdaWrapperX<UserFollowDO>()
                .selectAll(ApartmentDO.class)
                .leftJoin(ApartmentDO.class, ApartmentDO::getId, UserFollowDO::getFollowId)
                .eqIfPresent(UserFollowDO::getUserId, userId)
                .eqIfPresent(UserFollowDO::getType, 0)
                .orderByDesc(ApartmentDO::getId));
    }
}